package com.example.farmer.Adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.example.farmer.Entity.ChatMessage;
import com.example.farmer.R;

import java.util.List;

public class ReceivedMessageAdapter extends RecyclerView.Adapter<ReceivedMessageAdapter.ReceivedMessageViewHolder> {

    private List<ChatMessage> receivedMessages;
    private Context context;

    public ReceivedMessageAdapter(Context context, List<ChatMessage> receivedMessages) {
        this.context = context;
        this.receivedMessages = receivedMessages;
    }

    @NonNull
    @Override
    public ReceivedMessageViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.received_message_item, parent, false);
        return new ReceivedMessageViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull ReceivedMessageViewHolder holder, int position) {
        ChatMessage chatMessage = receivedMessages.get(position);
        holder.user_number.setText(chatMessage.getMessageContent());
        holder.timestamp.setText(formatTimestamp(chatMessage.getTimestamp()));
        // 可以添加更多的绑定逻辑，如时间戳、用户名称等
    }

    @Override
    public int getItemCount() {
        return receivedMessages.size();
    }

    private String formatTimestamp(long timestamp) {
        return new java.text.SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new java.util.Date(timestamp));
    }

    static class ReceivedMessageViewHolder extends RecyclerView.ViewHolder {
        ImageView user_image;
        TextView user_number;
        TextView timestamp;
        // 可以添加更多的视图组件，如时间戳、用户名称等

        ReceivedMessageViewHolder(View itemView) {
            super(itemView);
            user_number = itemView.findViewById(R.id.user_number);
            timestamp = itemView.findViewById(R.id.timestamp);
            user_image = itemView.findViewById(R.id.user_image);

            // 初始化更多的视图组件
        }
    }
    public void updateMessages(List<ChatMessage> newMessages) {
        receivedMessages.clear();
        receivedMessages.addAll(newMessages);
        notifyDataSetChanged();
    }
}
